package com.sneakxy.mybase4j.platform.repository;

import java.util.List;

import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.sneakxy.mybase4j.base.repository.BaseRepository;
import com.sneakxy.mybase4j.platform.beans.Router;

/**
 * 
 * @author 潜行的青衣
 * @see
 */
@Repository
public interface RouterRepository extends BaseRepository<Router, String> {

	@Select({
		"select r.name from t_mb4j_router r ", 
		"where r.perm_id in (select perm_id from t_mb4j_role_perm rp ",
		"left join t_mb4j_account_role ar on ar.role_id=rp.role_id ",
		"where ar.account_id=#{account_id}) or r.perm_id is null"})
	@ResultType(String.class)
	public List<String> findByAccountId(@Param("account_id") String account_id);
	
	@Update("update t_mb4j_router set perm_id=NULL where perm_id=#{permId}")
	public int updatePermIdToNull(@Param("permId") String permId);
	
	@Select({
		"select r.* from t_mb4j_router r ",
		"where name like #{search} or memo like #{search} or perm_id like #{search}"})
	@ResultMap("Router")
	public Page<Router> findBySearch(@Param("search") String search, Pageable pageable);
	
}
